package com.xm.xmapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.LoginRequest;
import com.xm.xmapp.response.bean.ResponseUserBean;
import com.xm.xmapp.tools.Des4;
import com.xm.xmapp.tools.Helper_SharedPreferences;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.Tools;

public class LoginActivity extends BaseActivity implements OnClickListener {

	private TextView zhuce, forget;
	private ImageView back;
	private EditText phone, password;
	private Button denglu;
	public static final String SAVE_PHONE = "save_phone";// 保存上一次登录的手机号

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
	}

	@Override
	public void initView() {
		denglu = (Button) findViewById(R.id.denglu);
		zhuce = (TextView) findViewById(R.id.zhuce);
		back = (ImageView) findViewById(R.id.back);
		phone = (EditText) findViewById(R.id.phone);
		password = (EditText) findViewById(R.id.password);
		forget = (TextView) findViewById(R.id.forget);
	}

	@Override
	public void initData() {

		String phone_number = Helper_SharedPreferences.get_str_sp(SAVE_PHONE);
		if (phone_number != null && !phone_number.equals(""))
			phone.setText(phone_number);

	}

	@Override
	public void initEvent() {
		back.setOnClickListener(this);
		denglu.setOnClickListener(this);
		zhuce.setOnClickListener(this);
		forget.setOnClickListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MyAppcation.getMyAppcation().reMoveRequest(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		case R.id.forget:
			intent = new Intent(MyAppcation.getMyAppcation(), ForGetPasswordActivity.class);
			startActivity(intent);
			break;

		case R.id.denglu:

			if (!StringTools.isPhone(phone.getText().toString())) {
				Tools.showMessage("请输入正确的手机号");
			} else if (!StringTools.isLength(password.getText().toString(), 6)) {
				Tools.showMessage("密码输入为6到16位");
			} else {
				LoginRequest requestBean = new LoginRequest();
				requestBean.jpush_id = MyAppcation.getMyAppcation().getJpush_id();
				requestBean.password = Des4.encode(password.getText().toString());
				requestBean.phone = phone.getText().toString();
				requestBean.phone_model = Tools.getMODEL();
				requestBean.phone_system_version = Tools.getSYSTEM_MODEL();
				MyAppcation.getMyAppcation().clearUserBean();
				promptDialog.show();
				MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

					@Override
					public void onResponse(String response) {

						promptDialog.dismiss();
						ResponseUserBean bean = JSON.parseObject(response, ResponseUserBean.class);
						MyAppcation.getMyAppcation().saveUserBean(bean);
						intent = new Intent(MyAppcation.getMyAppcation(), MainActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(intent);
						Helper_SharedPreferences.set_str_sp(SAVE_PHONE, phone.getText().toString());
					}
				}, new DisssmissInterFace() {

					@Override
					public void putError(VolleyError error) {
						promptDialog.dismiss();
					}
				});
			}
			break;

		case R.id.zhuce:
			intent = new Intent(MyAppcation.getMyAppcation(), RegisterActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
}
